home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Games / ADoomPPC / src / hu_lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-21  |  7.3 KB  |  357 lines

  1. // Emacs style mode select   -*- C++ -*- 
  2. //-----------------------------------------------------------------------------
  3. //
  4. // $Id:$
  5. //
  6. // Copyright (C) 1993-1996 by id Software, Inc.
  7. //
  8. // This source is available for distribution and/or modification
  9. // only under the terms of the DOOM Source Code License as
  10. // published by id Software. All rights reserved.
  11. //
  12. // The source is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
  15. // for more details.
  16. //
  17. // $Log:$
  18. //
  19. // DESCRIPTION:  heads-up text and input code
  20. //
  21. //-----------------------------------------------------------------------------
  22.  
  23. static const char
  24. rcsid[] = "$Id: hu_lib.c,v 1.3 1997/01/26 07:44:58 b1 Exp $";
  25.  
  26. #include <ctype.h>
  27.  
  28. #include "doomdef.h"
  29. #include "doomstat.h"
  30.  
  31. #include "v_video.h"
  32. #include "m_swap.h"
  33.  
  34. #include "hu_lib.h"
  35. #include "r_local.h"
  36. #include "r_draw.h"
  37.  
  38. // boolean : whether the screen is always erased
  39. #define noterased viewwindowx
  40.  
  41. extern boolean  automapactive;  // in AM_map.c
  42. extern int maponhu;        // checkparm of -maponhu
  43.  
  44. void HUlib_init(void)
  45. {
  46. }
  47.  
  48. void HUlib_clearTextLine(hu_textline_t* t)
  49. {
  50.     t->len = 0;
  51.     t->l[0] = 0;
  52.     t->needsupdate = true;
  53. }
  54.  
  55. void
  56. HUlib_initTextLine
  57. ( hu_textline_t*        t,
  58.   int                   x,
  59.   int                   y,
  60.   patch_t**             f,
  61.   int                   sc )
  62. {
  63.     t->x = x;
  64.     t->y = y;
  65.     t->f = f;
  66.     t->sc = sc;
  67.     HUlib_clearTextLine(t);
  68. }
  69.  
  70. boolean
  71. HUlib_addCharToTextLine
  72. ( hu_textline_t*        t,
  73.   char                  ch )
  74. {
  75.  
  76.     if (t->len == HU_MAXLINELENGTH)
  77.         return false;
  78.     else
  79.     {
  80.         t->l[t->len++] = ch;
  81.         t->l[t->len] = 0;
  82.         t->needsupdate = 4;
  83.         return true;
  84.     }
  85.  
  86. }
  87.  
  88. boolean HUlib_delCharFromTextLine(hu_textline_t* t)
  89. {
  90.  
  91.     if (!t->len) return false;
  92.     else
  93.     {
  94.         t->l[--t->len] = 0;
  95.         t->needsupdate = 4;
  96.         return true;
  97.     }
  98.  
  99. }
  100.  
  101. void
  102. HUlib_drawTextLine
  103. ( hu_textline_t*        l,
  104.   boolean               drawcursor )
  105. {
  106.  
  107.     int                 i;
  108.     int                 w;
  109.     int                 x;
  110.     unsigned char       c;
  111.  
  112.     // draw the new stuff
  113.     x = l->x;
  114.     for (i=0;i<l->len;i++)
  115.     {
  116.         c = toupper(l->l[i]);
  117.         if (c != ' '
  118.             && c >= l->sc
  119.             && c <= '_')
  120.         {
  121.             w = SWAPSHORT(l->f[c - l->sc]->width);
  122.             if (x+w > SCREENWIDTH)
  123.                 break;
  124.             V_DrawPatchDirect(x, l->y, FG, l->f[c - l->sc]);
  125.             x += w;
  126.         }
  127.         else
  128.         {
  129.             x += 4;
  130.             if (x >= SCREENWIDTH)
  131.                 break;
  132.         }
  133.     }
  134.  
  135.     // draw the cursor if requested
  136.     if (drawcursor
  137.         && x + SWAPSHORT(l->f['_' - l->sc]->width) <= SCREENWIDTH)
  138.     {
  139.         V_DrawPatchDirect(x, l->y, FG, l->f['_' - l->sc]);
  140.     }
  141. }
  142.  
  143.  
  144. // sorta called by HU_Erase and just better darn get things straight
  145. void HUlib_eraseTextLine(hu_textline_t* l)
  146. {
  147.     int                 lh;
  148.     int                 y;
  149.     int                 yoffset;
  150.     static boolean      lastautomapactive = true;
  151.  
  152.     // Only erases when NOT in automap and the screen is reduced,
  153.     // and the text must either need updating or refreshing
  154.     // (because of a recent change back from the automap)
  155.  
  156.     if ((!automapactive || maponhu) &&
  157.         viewwindowx && l->needsupdate)
  158.     {
  159.         lh = SWAPSHORT(l->f[0]->height) + 1;
  160.         for (y=l->y,yoffset=y*SCREENWIDTH ; y<l->y+lh ; y++,yoffset+=SCREENWIDTH)
  161.         {
  162.             if (y < viewwindowy || y >= viewwindowy + viewheight)
  163.                 R_VideoErase(yoffset, SCREENWIDTH); // erase entire line
  164.             else
  165.             {
  166.                 R_VideoErase(yoffset, viewwindowx); // erase left border
  167.                 R_VideoErase(yoffset + viewwindowx + viewwidth, viewwindowx);
  168.                 // erase right border
  169.             }
  170.         }
  171.     }
  172.  
  173.     lastautomapactive = automapactive;
  174.     if (l->needsupdate) l->needsupdate--;
  175.  
  176. }
  177.  
  178. void
  179. HUlib_initSText
  180. ( hu_stext_t*   s,
  181.   int           x,
  182.   int           y,
  183.   int           h,
  184.   patch_t**     font,
  185.   int           startchar,
  186.   boolean*      on )
  187. {
  188.  
  189.     int i;
  190.  
  191.     s->h = h;
  192.     s->on = on;
  193.     s->laston = true;
  194.     s->cl = 0;
  195.     for (i=0;i<h;i++)
  196.         HUlib_initTextLine(&s->l[i],
  197.                            x, y - i*(SWAPSHORT(font[0]->height)+1),
  198.                            font, startchar);
  199.  
  200. }
  201.  
  202. void HUlib_addLineToSText(hu_stext_t* s)
  203. {
  204.  
  205.     int i;
  206.  
  207.     // add a clear line
  208.     if (++s->cl == s->h)
  209.         s->cl = 0;
  210.     HUlib_clearTextLine(&s->l[s->cl]);
  211.  
  212.     // everything needs updating
  213.     for (i=0 ; i<s->h ; i++)
  214.         s->l[i].needsupdate = 4;
  215.  
  216. }
  217.  
  218. void
  219. HUlib_addMessageToSText
  220. ( hu_stext_t*   s,
  221.   char*         prefix,
  222.   char*         msg )
  223. {
  224.     HUlib_addLineToSText(s);
  225.     if (prefix)
  226.         while (*prefix)
  227.             HUlib_addCharToTextLine(&s->l[s->cl], *(prefix++));
  228.  
  229.     while (*msg)
  230.         HUlib_addCharToTextLine(&s->l[s->cl], *(msg++));
  231. }
  232.  
  233. void HUlib_drawSText(hu_stext_t* s)
  234. {
  235.     int i, idx;
  236.     hu_textline_t *l;
  237.  
  238.     if (!*s->on)
  239.         return; // if not on, don't draw
  240.  
  241.     // draw everything
  242.     for (i=0 ; i<s->h ; i++)
  243.     {
  244.         idx = s->cl - i;
  245.         if (idx < 0)
  246.             idx += s->h; // handle queue of lines
  247.         
  248.         l = &s->l[idx];
  249.  
  250.         // need a decision made here on whether to skip the draw
  251.         HUlib_drawTextLine(l, false); // no cursor, please
  252.     }
  253.  
  254. }
  255.  
  256. void HUlib_eraseSText(hu_stext_t* s)
  257. {
  258.  
  259.     int i;
  260.  
  261.     for (i=0 ; i<s->h ; i++)
  262.     {
  263.         if (s->laston && !*s->on)
  264.             s->l[i].needsupdate = 4;
  265.         HUlib_eraseTextLine(&s->l[i]);
  266.     }
  267.     s->laston = *s->on;
  268.  
  269. }
  270.  
  271. void
  272. HUlib_initIText
  273. ( hu_itext_t*   it,
  274.   int           x,
  275.   int           y,
  276.   patch_t**     font,
  277.   int           startchar,
  278.   boolean*      on )
  279. {
  280.     it->lm = 0; // default left margin is start of text
  281.     it->on = on;
  282.     it->laston = true;
  283.     HUlib_initTextLine(&it->l, x, y, font, startchar);
  284. }
  285.  
  286.  
  287. // The following deletion routines adhere to the left margin restriction
  288. void HUlib_delCharFromIText(hu_itext_t* it)
  289. {
  290.     if (it->l.len != it->lm)
  291.         HUlib_delCharFromTextLine(&it->l);
  292. }
  293.  
  294. void HUlib_eraseLineFromIText(hu_itext_t* it)
  295. {
  296.     while (it->lm != it->l.len)
  297.         HUlib_delCharFromTextLine(&it->l);
  298. }
  299.  
  300. // Resets left margin as well
  301. void HUlib_resetIText(hu_itext_t* it)
  302. {
  303.     it->lm = 0;
  304.     HUlib_clearTextLine(&it->l);
  305. }
  306.  
  307. void
  308. HUlib_addPrefixToIText
  309. ( hu_itext_t*   it,
  310.   char*         str )
  311. {
  312.     while (*str)
  313.         HUlib_addCharToTextLine(&it->l, *(str++));
  314.     it->lm = it->l.len;
  315. }
  316.  
  317. // wrapper function for handling general keyed input.
  318. // returns true if it ate the key
  319. boolean
  320. HUlib_keyInIText
  321. ( hu_itext_t*   it,
  322.   unsigned char ch )
  323. {
  324.  
  325.     if (ch >= ' ' && ch <= '_') 
  326.         HUlib_addCharToTextLine(&it->l, (char) ch);
  327.     else 
  328.         if (ch == KEY_BACKSPACE) 
  329.             HUlib_delCharFromIText(it);
  330.         else 
  331.             if (ch != KEY_ENTER) 
  332.                 return false; // did not eat key
  333.  
  334.     return true; // ate the key
  335.  
  336. }
  337.  
  338. void HUlib_drawIText(hu_itext_t* it)
  339. {
  340.  
  341.     hu_textline_t *l = &it->l;
  342.  
  343.     if (!*it->on)
  344.         return;
  345.     HUlib_drawTextLine(l, true); // draw the line w/ cursor
  346.  
  347. }
  348.  
  349. void HUlib_eraseIText(hu_itext_t* it)
  350. {
  351.     if (it->laston && !*it->on)
  352.         it->l.needsupdate = 4;
  353.     HUlib_eraseTextLine(&it->l);
  354.     it->laston = *it->on;
  355. }
  356.  
  357.